package com.itx.gatewayx.controller;

import com.itx.gatewayx.common.model.ResponseResult;
import com.itx.gatewayx.domain.RouteConfig;
import com.itx.gatewayx.service.IDynamicRouteService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.route.RouteDefinition;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

import java.util.List;

/**
 * 路由管理控制器
 *
 * @author itx
 */
@Slf4j
@RestController
@RequestMapping("/route")
@RequiredArgsConstructor
public class RouteController {

    private final IDynamicRouteService dynamicRouteService;

    /**
     * 获取所有路由配置
     *
     * @return 路由列表
     */
    @GetMapping("/list")
    public Mono<ResponseResult<List<RouteDefinition>>> list() {
        List<RouteDefinition> routes = dynamicRouteService.getRoutes();
        return Mono.just(ResponseResult.success(routes));
    }
    
    /**
     * 获取指定路由配置
     *
     * @param routeId 路由ID
     * @return 路由定义
     */
    @GetMapping("/{routeId}")
    public Mono<ResponseResult<RouteDefinition>> getRoute(@PathVariable String routeId) {
        RouteDefinition route = dynamicRouteService.getRoute(routeId);
        if (route != null) {
            return Mono.just(ResponseResult.success(route));
        } else {
            return Mono.just(ResponseResult.error("路由不存在"));
        }
    }
    
    /**
     * 添加路由
     *
     * @param routeConfig 路由配置
     * @return 结果
     */
    @PostMapping
    public Mono<ResponseResult<Boolean>> add(@RequestBody RouteConfig routeConfig) {
        boolean result = dynamicRouteService.addRoute(routeConfig);
        if (result) {
            return Mono.just(ResponseResult.success(true));
        } else {
            return Mono.just(ResponseResult.error("添加路由失败"));
        }
    }
    
    /**
     * 更新路由
     *
     * @param routeConfig 路由配置
     * @return 结果
     */
    @PutMapping
    public Mono<ResponseResult<Boolean>> update(@RequestBody RouteConfig routeConfig) {
        boolean result = dynamicRouteService.updateRoute(routeConfig);
        if (result) {
            return Mono.just(ResponseResult.success(true));
        } else {
            return Mono.just(ResponseResult.error("更新路由失败"));
        }
    }
    
    /**
     * 删除路由
     *
     * @param routeId 路由ID
     * @return 结果
     */
    @DeleteMapping("/{routeId}")
    public Mono<ResponseResult<Boolean>> delete(@PathVariable String routeId) {
        boolean result = dynamicRouteService.deleteRoute(routeId);
        if (result) {
            return Mono.just(ResponseResult.success(true));
        } else {
            return Mono.just(ResponseResult.error("删除路由失败"));
        }
    }
    
    /**
     * 刷新路由
     *
     * @return 结果
     */
    @PostMapping("/refresh")
    public Mono<ResponseResult<Boolean>> refresh() {
        boolean result = dynamicRouteService.refreshRoutes();
        if (result) {
            return Mono.just(ResponseResult.success(true));
        } else {
            return Mono.just(ResponseResult.error("刷新路由失败"));
        }
    }
} 